﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using eCommMongo.Core;
using eCommMongo.Core.Domain.Posts;
using eCommMongo.Service.Posts;
using eCommMongo.Web.Models.Posts;
using yiCommerce.Core;
using yiCommerce.Service.Messages;

namespace eCommMongo.Web.Controllers.api
{


   

    public class PostApiController : ApiController
    {
        private IPostService _postService;
        private IWorkContext _workContext;
        private ICommentService _commentService;
        private IMessageService _messageService;

        public PostApiController(IPostService statusService, IWorkContext workContext, ICommentService commentService, IMessageService messageService)
        {
            _postService = statusService;
            _workContext = workContext;
            _commentService = commentService;
            _messageService = messageService;
        }


        [HttpPost]
        public GivePointResult GivePointToStatuses(GivePointRequest request)
        {
            var result = new GivePointResult();
            try
            {
                _postService.GivePointToPost(request.PostId, _workContext.CurrentCustomer.Id, 1);
                var newPoint = _postService.GetPointOfStatus(request.PostId);
                result.CurrentPoint = newPoint.HasValue? newPoint.Value: 0;
                
                result.Status = ConstInfo.ResultSuccessStatus;
                result.Message = _messageService.GetResourceValue(Messages.GivePointSuccessfully, 0);
            }
            catch (Exception e)
            {
                result.Message = e.Message;
                result.Status = ConstInfo.ResultFailedStatus;
            }

          
            return result;

        }

        [HttpPost]
        public HomeLineResult HomeLine(HomeLineRequest request)
        {
            var result = new HomeLineResult();
            //var lastPostDate = request.LastPostDate==null? DateTime.MinValue: DateTime.Parse(request.LastPostDate);
            long? lastPostDate = null;
            if(!string.IsNullOrEmpty(request.LastPostDate ))
            {
                lastPostDate = long.Parse(request.LastPostDate);
            };

            var posts = _postService.HomeTimeLine(request.CustomerId, request.PageIndex, request.PageSize, lastPostDate);
            //var statusList = Mapper.PostMapper.PrepareStatusListItemModel(statuses);
            result.PostList = posts;
            result.Status = "0";
            return result;

        }

        [HttpPost]
        public HomeLineProductResult HomeLineProduct(HomeLineProductRequest request)
        {
            var result = new HomeLineProductResult();
            var posts = _postService.HomeTimeLineProduct(request.CustomerId, request.PageIndex, request.PageSize);
            //var statusList = Mapper.StatusMapper.PrepareStatusListItemModel(statuses);
            result.PostList = posts;
            result.Status = "0";
            return result;

        }


        [HttpPost]
        public GetPageCommentsResult GetPageComments(GetPageCommentsRequest request)
        {
            var result = new GetPageCommentsResult();
            var comments = _postService.GetPagedComments(request.PostId, request.PageIndex, request.PageSize);
            //var commentModels = yiCommerce.Mapper.StatusMapper.ToCommentModel(comments);
            result.CommentList = comments;
            result.Status = "0";
            return result;
        }


    }

    public class HomeLineProductResult
    {
        public IList<Post> PostList { get; set; }

        public string Status { get; set; }
    }

    public class HomeLineProductRequest
    {
        public string CustomerId { get; set; }

        public int PageSize { get; set; }

        public int PageIndex { get; set; }
    }

    public class GivePointResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
        public int CurrentPoint { get; set; }
    }

    public class GivePointRequest
    {
        public string PostId { get; set; }

    }

    public class GetPageCommentsRequest
    {
        public string PostId{ get; set; }

        public int PageSize { get; set; }

        public int PageIndex { get; set; }
    }

    public class GetPageCommentsResult
    {
        public IList<Comment> CommentList { get; set; }

        public string Status { get; set; }
    }


    public class HomeLineResult
    {
        public IList<Post> PostList { get; set; }

        public string Status { get; set; }
    }

    public class HomeLineRequest
    {
        public string LastPostDate { get; set; }
        public string CustomerId { get; set; }

        public int PageSize { get; set; }

        public int PageIndex { get; set; }
    }
}
